package com.sun.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.entity.Product;
import com.sun.entity.R;
import com.sun.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SunRui
 * @since 2024-12-10
 */
@RestController
@RequestMapping("/product")
@Tag(name = "商品控制器",description = "商品接口")
public class ProductController {

    @Autowired
    IProductService productService;

    // 新增
    @PostMapping
    @Operation(summary = "新增商品",description = "新增商品接口")
    @ApiResponses(value = {@ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "404",description = "失败")})
    public R insert(@Parameter(description = "商品信息") @RequestBody Product product){
        return R.success(productService.insert(product));
    }


    // 修改
    @PutMapping
    @Operation(summary = "修改商品",description = "新增商品接口")
    @ApiResponses(value = {@ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "404",description = "失败")})
    public R update(@Parameter(description = "商品信息") @RequestBody Product product){
        return R.success(productService.update(product));
    }

    // 删除
    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品",description = "删除商品接口")
    @ApiResponses(value = {@ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "404",description = "失败")})
    public R delete(@Parameter(description = "商品id") @Validated Integer id){
        return R.success(productService.delete(id));
    }

    // 查询所有
    @GetMapping
    @Operation(summary = "查询所有商品",description = "查询所有商品接口")
    @ApiResponses(value = {@ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "404",description = "失败")})
    public R selectAll(){
        return R.success(productService.selectAll());
    }

    // 根据Id查询
    @GetMapping("/{id}")
    @Operation(summary = "根据Id查询商品",description = "根据Id查询商品接口")
    @ApiResponses(value = {@ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "404",description = "失败")})
    public R selectById(@Parameter(description = "商品id") @Validated Integer id){
        return R.success(productService.selectById(id));
    }

    // 分页查询
    @GetMapping("/page")
    @Operation(summary = "分页查询商品",description = "分页查询商品接口")
    @ApiResponses(value = {@ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "404",description = "失败")})
    public R selectPage(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer size){
        return R.success(productService.selectPage(page,size));
    }
}
